<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  
<html xmlns = "http://www.w3.org/1999/xhtml">
  <!-- 2012 lukbrasil -->
  <!-- Internet & World Wide Web How To Program - Exercise 7.27 -->
  <head>
    <title>Exercise 7.27</title>
    
    <script type = "text/javascript">
      <!--
      var number;
      var d1;
      var d2;
      var d3;
      var d4;
      var aux;
      
      number = parseInt(window.prompt("Enter a 4-digit integer (-1 to quit)", "-1"));
      while (number != -1) {
        
        while (parseInt(number / 10000) != 0) {
          window.alert("Number entered is too long.");
          number = parseInt(window.prompt("Enter a 4-digit integer (-1 to quit)", "-1"));
        }
        
        if (number != -1) {        
          d1 = parseInt(number / 1000);
          d2 = parseInt(number / 100) % 10;
          d3 = parseInt(number / 10) % 10;
          d4 = number % 10;
          
          // Replace each digit by (the sum of that digit plus 7) modulus 10
          
          d1 = (d1 + 7) % 10;
          d2 = (d2 + 7) % 10;
          d3 = (d3 + 7) % 10;
          d4 = (d4 + 7) % 10;
          
          // Swap 1st and 3rd digit
          
          aux = d1;
          d1 = d3;
          d3 = aux
          
          // Swap 2nd and 4th digit
          
          aux = d2;
          d2 = d4;
          d4 = aux;
          
          number = d1 * 1000 + d2 * 100 + d3 * 10 + d4;
          
          document.writeln("Encrypted integer is " + number + "<br/>");
          
          number = parseInt(window.prompt("Enter a 4-digit integer (-1 to quit)", "-1"));
        }
      }
      // -->
    </script>
  </head>
  <body>
  </body>
</html>
